<?php
	
	abstract class module {
		
		#protected $name = "__undefined";
		protected $name;
		protected $action_list = array();

		public function __construct(){
			
		}

		public function exec(){
			$this->dispatch_action();
		}
		
		public function get_name(){
			return $this->name;
		}
		
		public function set_name($name){
			if (!isset($this->name)){
				$this->name = $name;
			}
		}
		
		public function fetch(){
			return $this->exec();
		}
		
		//--------------------------------------------------------------
		//wrappers start
		//--------------------------------------------------------------
		//todo: add array 'param' pseudoaccessor, like $this->param["foo"];
		
		public function get_param($name,$type = null){
			return context::get("io")->get($name,$type);
		}
		
		//--------------------------------------------------------------
		
		
		//link undefined get to context service
		public function __get($name){
			#if ($handle = context::$name() ){
			if ($handle = context::get($name) ){
				return $handle;
			}
		}
		
		//dispatch undefined call to the first valid service
		public function __call($name, $params){
			#return context::__callStatic($name,$params);
			return context::call($name,$params);
		}
		
		public function __default(){
			//do nothing
		}
		
		public function dispatch_action($__action_list = null){
			$action = context::get("io")->get("action");		//todo: change me to request->get_action
			
			$action_list = $__action_list ? $__action_list : $this->action_list;
				
			if (in_array($action,$action_list)){
				$__cb = array($this,"__{$action}");
				if (is_callable($__cb)){
					call_user_func($__cb);
				}
			}else{
				$this->__default();	//call default if not dispatched
			}
		}

		public function set_page($page){
			header("Location: ?page={$page}");
			die();
		}
		
	}
?>
